package com.lgmshare.k3.manager;

import android.content.Context;
import android.text.TextUtils;

import com.lgmshare.component.utils.ContextUtils;
import com.lgmshare.component.utils.JSONUtils;
import com.lgmshare.component.utils.SharedPreferencesUtils;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.model.UserInfo;

/**
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2016/6/23 21:05
 */
public class UserInfoManager {

    private static final String TAG = "UserInfoManager";

    private static final String KEY_USER = "user";
    private static final String KEY_USER_USERNAME = "user_username";
    private static final String KEY_USER_UDID = "user_udid";

    private Context mContext;
    private UserInfo mUserInfo;
    private String mUDID;
    private String mPushClientID;


    private final static class HolderClass {
        private final static UserInfoManager INSTANCE = new UserInfoManager();
    }

    public static UserInfoManager getInstance() {
        return HolderClass.INSTANCE;
    }

    /**
     * 初始化
     */
    public UserInfoManager() {
        this.mContext = UIUtils.getContext();
        this.mUserInfo = getCacheUser();
    }

    /**
     * 添加user
     *
     * @param userInfo 用户对象
     */
    public void setUserInfo(UserInfo userInfo) {
        mUserInfo = userInfo;
        saveCacheUser(userInfo);
        saveUsername(userInfo.getUsername());
        AppDataSyncManager.getInstance().sendUserInfoUpdateBroadcast(userInfo);
    }

    public synchronized UserInfo getUserInfo() {
        if (mUserInfo == null) {
            mUserInfo = getCacheUser();
        }
        return mUserInfo;
    }

    /**
     * 获取缓存user数据
     *
     * @return
     */
    private UserInfo getCacheUser() {
        String data = SharedPreferencesUtils.getString(KEY_USER);
        return JSONUtils.parseObject(data, UserInfo.class);
    }

    /**
     * 缓存user
     *
     * @param userInfo
     */
    private void saveCacheUser(UserInfo userInfo) {
        SharedPreferencesUtils.putString(KEY_USER, JSONUtils.toJSONString(userInfo));
    }

    /**
     * 获取上一次登录账户名
     *
     * @return
     */
    public String getUsername() {
        return SharedPreferencesUtils.getString(KEY_USER_USERNAME);
    }

    /**
     * 保存登录账户名
     *
     * @param username
     */
    private void saveUsername(String username) {
        SharedPreferencesUtils.putString(KEY_USER_USERNAME, username);
    }

    public String getUDID() {
        if (TextUtils.isEmpty(mUDID)) {
            mUDID = SharedPreferencesUtils.getString(KEY_USER_UDID);
        }
        if (TextUtils.isEmpty(mUDID)) {
            mUDID = ContextUtils.getDeviceID(mContext);
            SharedPreferencesUtils.putString(KEY_USER_UDID, mUDID);
        }
        return mUDID;
    }

    public String getPushClientID() {
        return mPushClientID;
    }

    public void setPushClientID(String pushClientID) {
        mPushClientID = pushClientID;
    }

    public boolean isLogin() {
        return mUserInfo != null && !TextUtils.isEmpty(mUserInfo.getUsername());
    }

    /**
     * 用户登出
     */
    public void logout() {
        mUserInfo = null;
        SharedPreferencesUtils.remove(KEY_USER);
        AppDataSyncManager.getInstance().sendUserLoginStatusChangeBroadcast(false);
    }

}
